#include "menu.h"
#include "oled.h"
#include "rtc.h"
#include <stdio.h>
#include "stdlib.h"
#include "timer.h"
#include "key.h"
#include "led.h"
#include "delay.h"
#include "pwm.h"

static void OLED_ShowMainPage(void *arg);
static void OLED_ShowTimeSettingSelect(void *arg);
static void OLED_ShowDateSettingSelect(void *arg);
static void OLED_ShowAlarmSettingSelect(void *arg);
static void OLED_ShowPlusSettingSelect(void *arg);
static void OLED_ShowPWMSettingSelect(void *arg);
static void OLED_ShowExitSelect(void *arg);

static void OLED_ShowTimeSetting_Hour(void *arg);
static void OLED_ShowTimeSetting_Min(void *arg);
static void OLED_ShowTimeSetting_Sec(void *arg);
static void OLED_ShowTimeSetting_Save(void *arg);
static void OLED_ShowTimeSetting_Exit(void *arg);

static void OLED_ShowDateSetting_Year(void *arg);
static void OLED_ShowDateSetting_Month(void *arg);
static void OLED_ShowDateSetting_Day(void *arg);
static void OLED_ShowDateSetting_Save(void *arg);
static void OLED_ShowDateSetting_Exit(void *arg);

static void OLED_ShowAlarmSetting_Hour(void *arg);
static void OLED_ShowAlarmSetting_Min(void *arg);
static void OLED_ShowAlarmSetting_Sec(void *arg);
static void OLED_ShowAlarmSetting_Save(void *arg);
static void OLED_ShowAlarmSetting_Exit(void *arg);

static void OLED_ShowPlusSetting_Width(void *arg);
static void OLED_ShowPlusSetting_Polarity(void *arg);
static void OLED_ShowPlusSetting_Trigger(void *arg);
static void OLED_ShowPlusSetting_Exit(void *arg);


static void OLED_ShowPWMSetting_Cycle(void *arg);
static void OLED_ShowPWMSetting_Duty(void *arg);
static void OLED_ShowPWMSetting_Exit(void *arg);




tTime g_tTimeTmp;
tTime g_tAlarmTmp;
u32   g_dwPlusWidthMs = 40;
u8    g_cPlusPolarity = 1;
u8    g_cFlag = 0;


T_MenuTable g_taMenuTable[] = {
	/* 第1层:主界面 */
	{MAIN_INDEX, SET_TIME_INDEX, MAIN_INDEX, MAIN_INDEX, MAIN_INDEX, (*OLED_ShowMainPage)}, /* 主界面, 显示时钟 */	

	/* 第2层:菜单 , 下面 “[]” 表示选中 */
	{SET_TIME_INDEX, SET_DATE_INDEX, SET_TIME_INDEX, SET_TIME_INDEX, TIME_HOUR_INDEX,  (*OLED_ShowTimeSettingSelect)},   /* 显示:[1.时间设置]        2.日期设置       3.闹钟设置       4.退出    */    
	{SET_DATE_INDEX, SET_ALAR_INDEX, SET_DATE_INDEX, SET_DATE_INDEX, DATE_YEAR_INDEX,  (*OLED_ShowDateSettingSelect)},   /* 显示: 1.时间设置  [2.日期设置]  3.闹钟设置                 4.退出    */
	{SET_ALAR_INDEX, SET_PLUS_INDEX, SET_ALAR_INDEX, SET_ALAR_INDEX, ALARM_HOUR_INDEX, (*OLED_ShowAlarmSettingSelect)},  /* 显示: 1.时间设置   2.日期设置  [3.闹钟设置]  4.退出  */
	{SET_PLUS_INDEX, SET_PWM_INDEX,  SET_PLUS_INDEX, SET_PLUS_INDEX, PLUS_WIDTH_INDEX, (*OLED_ShowPlusSettingSelect)},
	{SET_PWM_INDEX,  SET_EXIT_INDEX, SET_PWM_INDEX,  SET_PWM_INDEX,  PWM_CYCLE_INDEX,  (*OLED_ShowPWMSettingSelect)},
	{SET_EXIT_INDEX, SET_TIME_INDEX, SET_EXIT_INDEX, SET_EXIT_INDEX, MAIN_INDEX,       (*OLED_ShowExitSelect)},          /* 显示: 1.时间设置         2.日期设置       3.闹钟设置      [4.退出] */

	/* 第3.1层: 时间设置 */
	{TIME_HOUR_INDEX, TIME_MIN_INDEX,  TIME_HOUR_INDEX, TIME_HOUR_INDEX, TIME_HOUR_INDEX, (*OLED_ShowTimeSetting_Hour)},  /* 选择[时] */
	{TIME_MIN_INDEX,  TIME_SEC_INDEX,  TIME_MIN_INDEX,  TIME_MIN_INDEX,  TIME_MIN_INDEX,  (*OLED_ShowTimeSetting_Min)},   /* 选择[分] */ 
	{TIME_SEC_INDEX,  TIME_SAVE_INDEX, TIME_SEC_INDEX,  TIME_SEC_INDEX,  TIME_SEC_INDEX,  (*OLED_ShowTimeSetting_Sec)},   /* 选择[秒] */
	{TIME_SAVE_INDEX, TIME_EXIT_INDEX, TIME_SAVE_INDEX, TIME_SAVE_INDEX, TIME_SAVE_INDEX, (*OLED_ShowTimeSetting_Save)},  /* 选择[保存] */
	{TIME_EXIT_INDEX, TIME_HOUR_INDEX, TIME_EXIT_INDEX, TIME_EXIT_INDEX, SET_TIME_INDEX,  (*OLED_ShowTimeSetting_Exit)},  /* 选择[退出] */
 
	/* 第3.2层: 日期设置       */
	{DATE_YEAR_INDEX,  DATE_MONTH_INDEX, DATE_YEAR_INDEX,  DATE_YEAR_INDEX,  DATE_YEAR_INDEX, (*OLED_ShowDateSetting_Year)},  /* 选择[年] */
	{DATE_MONTH_INDEX, DATE_DAY_INDEX,   DATE_MONTH_INDEX, DATE_MONTH_INDEX, DATE_MONTH_INDEX,  (*OLED_ShowDateSetting_Month)}, /* 选择[月] */
	{DATE_DAY_INDEX,   DATE_SAVE_INDEX,  DATE_DAY_INDEX,   DATE_DAY_INDEX,   DATE_DAY_INDEX,  (*OLED_ShowDateSetting_Day)},   /* 选择[日] */
	{DATE_SAVE_INDEX,  DATE_EXIT_INDEX,  DATE_SAVE_INDEX,  DATE_SAVE_INDEX,  DATE_SAVE_INDEX, (*OLED_ShowDateSetting_Save)},  /* 选择[保存] */
	{DATE_EXIT_INDEX,  DATE_YEAR_INDEX,  DATE_EXIT_INDEX,  DATE_EXIT_INDEX,  SET_DATE_INDEX,  (*OLED_ShowDateSetting_Exit)},  /* 选择[退出] */

	/* 第3.3层：闹钟设置 */
	{ALARM_HOUR_INDEX, ALARM_MIN_INDEX,  ALARM_HOUR_INDEX, ALARM_HOUR_INDEX, ALARM_HOUR_INDEX, (*OLED_ShowAlarmSetting_Hour)},  /* 选择[时] */
	{ALARM_MIN_INDEX,  ALARM_SEC_INDEX,  ALARM_MIN_INDEX,  ALARM_MIN_INDEX,  ALARM_MIN_INDEX,  (*OLED_ShowAlarmSetting_Min)},   /* 选择[分] */ 
	{ALARM_SEC_INDEX,  ALARM_SAVE_INDEX, ALARM_SEC_INDEX,  ALARM_SEC_INDEX,  ALARM_SEC_INDEX,  (*OLED_ShowAlarmSetting_Sec)},   /* 选择[秒] */
	{ALARM_SAVE_INDEX, ALARM_EXIT_INDEX, ALARM_SAVE_INDEX, ALARM_SAVE_INDEX, ALARM_SAVE_INDEX, (*OLED_ShowAlarmSetting_Save)},  /* 选择[保存] */
	{ALARM_EXIT_INDEX, ALARM_HOUR_INDEX, ALARM_EXIT_INDEX, ALARM_EXIT_INDEX, SET_ALAR_INDEX,   (*OLED_ShowAlarmSetting_Exit)},  /* 选择[退出] */

	/* 第3.4层：脉冲设置 */
	{PLUS_WIDTH_INDEX, PLUS_POLAR_INDEX, PLUS_WIDTH_INDEX, PLUS_WIDTH_INDEX, PLUS_WIDTH_INDEX, (*OLED_ShowPlusSetting_Width)},
	{PLUS_POLAR_INDEX, PLUS_TRIG_INDEX,  PLUS_POLAR_INDEX, PLUS_POLAR_INDEX, PLUS_POLAR_INDEX, (*OLED_ShowPlusSetting_Polarity)},
	{PLUS_TRIG_INDEX,  PLUS_EXIT_INDEX,  PLUS_TRIG_INDEX,  PLUS_TRIG_INDEX,  PLUS_TRIG_INDEX,  (*OLED_ShowPlusSetting_Trigger)},
	{PLUS_EXIT_INDEX,  PLUS_WIDTH_INDEX, PLUS_EXIT_INDEX,  PLUS_EXIT_INDEX,  SET_PLUS_INDEX,   (*OLED_ShowPlusSetting_Exit)},

	/* 第3.5层：PWM设置 */
	{PWM_CYCLE_INDEX, PWM_DUTY_INDEX,  PWM_CYCLE_INDEX, PWM_CYCLE_INDEX, PWM_CYCLE_INDEX, (*OLED_ShowPWMSetting_Cycle)},
	{PWM_DUTY_INDEX,  PWM_EXIT_INDEX,  PWM_DUTY_INDEX,  PWM_DUTY_INDEX,  PWM_DUTY_INDEX,  (*OLED_ShowPWMSetting_Duty)},
	{PWM_EXIT_INDEX,  PWM_CYCLE_INDEX, PWM_EXIT_INDEX,  PWM_EXIT_INDEX,  SET_PWM_INDEX,   (OLED_ShowPWMSetting_Exit)},
};


static void OLED_ShowSetTimeOpr(u8 x, u8 y, u8 mode)
{
	/* 1.时间设置 */
	u8 index[] = {14, 15, 16, 17};
	u8 buf[3];
	
	sprintf((char*)buf,"%d.", SET_TIME_INDEX);
	OLED_ShowString(x, y, buf, 16, mode);
	OLED_ShowChineseString(x + 16, y, index, sizeof(index), 16, mode);
	OLED_Fill(x + 16 + 64, y, 16, mode);
}

static void OLED_ShowSetDateOpr(u8 x, u8 y, u8 mode)
{
	/* 2.日期设置 */
	u8 index[] = {10, 3, 16, 17};
	u8 buf[3];
	
	sprintf((char*)buf,"%d.", SET_DATE_INDEX);
	OLED_ShowString(x, y, buf, 16, mode);
	OLED_ShowChineseString(x + 16, y, index, sizeof(index), 16, mode);
	OLED_Fill(x + 16 + 64, y, 16, mode);
}

static void OLED_ShowSetAlarmOpr(u8 x, u8 y, u8 mode)
{
	/* 3.闹钟设置 */
	u8 index[] = {18, 19, 16, 17};
	u8 buf[3];
	
	sprintf((char*)buf,"%d.", SET_ALAR_INDEX);
	OLED_ShowString(x, y, buf, 16, mode);
	OLED_ShowChineseString(x + 16, y, index, sizeof(index), 16, mode);
	OLED_Fill(x + 16 + 64, y, 16, mode);

}


static void OLED_ShowSetPlusOpr(u8 x, u8 y, u8 mode)
{
	/* 4.脉冲设置 */
	u8 index[] = {24, 25, 16, 17};
	u8 buf[3];
	
	sprintf((char*)buf,"%d.", SET_PLUS_INDEX);
	OLED_ShowString(x, y, buf, 16, mode);
	OLED_ShowChineseString(x + 16, y, index, sizeof(index), 16, mode);
	OLED_Fill(x + 16 + 64, y, 16, mode);

}

static void OLED_ShowSetPWMOpr(u8 x, u8 y, u8 mode)
{
	/* 5.脉冲设置 */
	u8 index[] = {16, 17};
	u8 buf[3];
	
	sprintf((char*)buf,"%d.", SET_PWM_INDEX);
	OLED_ShowString(x, y, buf, 16, mode);
	OLED_ShowString(x+16, y, "PWM", 16, mode);
	OLED_ShowChineseString(x + 16 + 24, y, index, sizeof(index), 16, mode);
	OLED_Fill(x + 16 + 24 + 32, y, 16, mode);

}


static void OLED_ShowSetExitOpr(u8 x, u8 y, u8 mode)
{
	/* 6.退出 */
	u8 index[] = {22, 23};
	u8 buf[3];
	
	sprintf((char*)buf,"%d.", SET_EXIT_INDEX);
	OLED_ShowString(x, y, buf, 16, mode);
	OLED_ShowChineseString(x + 16, y, index,  sizeof(index), 16, mode);
	OLED_Fill(x + 16 + 32, y, 16, mode);

}

static void OLED_ShowSave(u8 x, u8 y, u8 mode)
{
	/* 保存 */
	u8 index[] = {20, 21};
	OLED_ShowChineseString(x, y, index, sizeof(index), 16, mode);
}

static void OLED_ShowExit(u8 x, u8 y, u8 mode)
{
	/* 退出 */
	u8 index[] = {22, 23};
	OLED_ShowChineseString(x, y, index, sizeof(index), 16, mode);
}

static void OLED_ShowTimeSelet(ptTime pTime, u8 select)
{
	u8 buf[9];
	u8 posx1 = (128 - 16 * 8) / 2;

	switch (select)
	{
		case 0: /* 选择时 */
		{
			sprintf((char*)buf,"%02d", (int)pTime->hour);
			OLED_ShowString(posx1, 2, buf, 32, 0);
			sprintf((char*)buf,":%02d:%02d", (int)pTime->min, (int)pTime->sec);
			OLED_ShowString(posx1 + 32, 2, buf, 32, 1);
			break;
		}
		case 1: /* 选择分 */
		{
			sprintf((char*)buf,"%02d:", (int)pTime->hour);
			OLED_ShowString(posx1, 2, buf, 32, 1);
			sprintf((char*)buf,"%02d", (int)pTime->min);
			OLED_ShowString(posx1 + 48, 2, buf, 32, 0);
			sprintf((char*)buf,":%02d", (int)pTime->sec);
			OLED_ShowString(posx1 + 80, 2, buf, 32, 1);
			break;
		}
		case 2:/* 选择秒 */
		{
			sprintf((char*)buf,"%02d:%02d:",(int)pTime->hour, (int)pTime->min);
	        OLED_ShowString(posx1, 2, buf, 32, 1);
			sprintf((char*)buf,"%02d",(int)pTime->sec);
			OLED_ShowString(posx1 + 96, 2, buf, 32, 0);
			break;
		}
		default:
			break;
	}
}


static void OLED_ShowTime(u8 x, u8 y, u8 size, ptTime pTime)
{
	u8 buf[9];
	sprintf((char*)buf,"%02d:%02d:%02d",(int)pTime->hour, (int)pTime->min, (int)pTime->sec);
	OLED_ShowString(x, y, buf, size, 1);
}

static void OLED_ShowYearMothDay(u8 x, u8 y, u8 size, ptTime pTime)
{
	u8 buf[5];
	
	/* 显示格式：xxxx年xx月xx日 */
	u8 posx1 = x;                      /* “xxxx”起始x坐标 */
	u8 posx2 = posx1 + 4 * (size / 2); /* “年”的起始x坐标，由于xxxx是ascii码,点阵的宽度为字体大小的一半*/
	u8 posx3 = posx2 + size;           /* “xx”的起始x坐标, 由于年是中文点阵，字体宽度跟字体大小一样 */
	u8 posx4 = posx3 + 2 * (size / 2); /* “月”的起始x坐标 */
	u8 posx5 = posx4 + size;           /* “xx”的起始x坐标 */
	u8 posx6 = posx5 + 2 * (size / 2); /* “日”的起始x坐标 */
		
	sprintf((char*)buf,"%04d", (int)pTime->year);
	OLED_ShowString(posx1, y, buf, size, 1);
	OLED_ShowChinese(posx2, y, 0, size, 1); /* 年 */
	sprintf((char*)buf, "%02d", (int)pTime->moth);
	OLED_ShowString(posx3, y, buf, size, 1);
	OLED_ShowChinese(posx4, y, 1, size, 1); /* 月 */
	sprintf((char*)buf, "%02d", (int)pTime->day);
	OLED_ShowString(posx5, y, buf, size, 1);
	OLED_ShowChinese(posx6, y, 10, size, 1); /* 日 */
}

static void OLED_ShowWeek(u8 x, u8 y, u8 size, ptTime pTime)
{
	/* 显示格式：星期x */    
	u8 index[3] = {2, 3};
	if (pTime->week == 0) index[2] = (7 + 3);
	else index[2] =  (pTime->week + 3);
	OLED_ShowChineseString(x, y, index, sizeof(index), size, 1);
}


static void OLED_ShowTemp(u8 x, u8 y, u8 size, ptTemp pTemp)
{
	u8 buf[4];
	u8 posx1 = 128 - size;  /* ℃的起始x坐标 */
	u8 posx2;

	if (pTemp->Pos) /* 正温度 */
	{
		
		if (pTemp->Int < 10) 
		{
			posx2 = posx1 - size / 2;   /* 温度为1位数 */
		}
		else if (pTemp->Int >= 10 && pTemp->Int < 100) 
		{
			posx2 = posx1 - size / 2 * 2;   /* 温度为2位数 */
		}
		else if (pTemp->Int >= 100) 
		{
			posx2 = posx1 - size / 2 * 3;   /* 温度为3位数 */
		}
		sprintf((char *)buf, "%d", (int)pTemp->Int);
		OLED_ShowString(posx2, y, buf, size, 1);
	}
	else /* 负温度 */
	{
		if (pTemp->Int < 10) 
		{
			posx2 = posx1 - size / 2 * 2;   /* 温度为1位数, 加上“-”号 */
		}
		else if (pTemp->Int >= 10 && pTemp->Int < 100) 
		{
			posx2 = posx1 - size / 2 * 3;   /* 温度为2位数, 加上“-”号*/
		}
		sprintf((char *)buf, "-%d", (int)pTemp->Int);
		OLED_ShowString(posx2, y, buf, size, 1);
	}
	OLED_ShowChinese(posx1, y, 13, size, 1); /* ℃ */
}


static void OLED_ShowMainPage(void *arg)
{	
	tTemp temp;
	temp.Int = 25;
	temp.Dec = 0;
	temp.Pos = 1;
	
	if (ReflashTimeFlag == 1)
	{
		ReflashTimeFlag = 0;
		
		/* 显示年月日、星期 */
		OLED_ShowYearMothDay(8, 0, 16, &g_tTime);
		OLED_ShowWeek(0, 6, 16, &g_tTime);
		
		/* 显示时间 */
		OLED_ShowTime(0, 2, 32, &g_tTime);
		
		/* 显示温度 */
		OLED_ShowTemp(50, 6, 16, &temp);
	}

}

static void OLED_ShowTimeSettingSelect(void *arg)
{
	/* 显示:[1.时间设置]        2.日期设置      3.闹钟设置      4.退出   */ 
	OLED_ShowSetTimeOpr(0, 0, 0);
	OLED_ShowSetDateOpr(0, 2, 1);
	OLED_ShowSetAlarmOpr(0, 4, 1);
	OLED_ShowSetPlusOpr(0, 6, 1);
	//OLED_ShowSetExitOpr(0, 6, 1);
	
}

static void OLED_ShowDateSettingSelect(void *arg)
{
	/* 显示: 1.时间设置        [2.日期设置]     3.闹钟设置      4.退出   */
	OLED_ShowSetTimeOpr(0, 0, 1);
	OLED_ShowSetDateOpr(0, 2, 0);
	OLED_ShowSetAlarmOpr(0, 4, 1);
	OLED_ShowSetPlusOpr(0, 6, 1);
	//OLED_ShowSetExitOpr(0, 6, 1);

}
static void OLED_ShowAlarmSettingSelect(void *arg)
{
	/* 显示: 1.时间设置        2.日期设置     [3.闹钟设置]      4.退出   */
	OLED_ShowSetTimeOpr(0, 0, 1);
	OLED_ShowSetDateOpr(0, 2, 1);
	OLED_ShowSetAlarmOpr(0, 4, 0);
	OLED_ShowSetPlusOpr(0, 6, 1);
	//OLED_ShowSetExitOpr(0, 6, 1);
}

static void OLED_ShowPlusSettingSelect(void *arg)
{
	/* 显示: 1.时间设置        2.日期设置     [3.闹钟设置]      4.退出   */
	OLED_ShowSetTimeOpr(0, 0, 1);
	OLED_ShowSetDateOpr(0, 2, 1);
	OLED_ShowSetAlarmOpr(0, 4, 1);
	OLED_ShowSetPlusOpr(0, 6, 0);	
}

static void OLED_ShowPWMSettingSelect(void *arg)
{
	OLED_ShowSetPWMOpr(0, 0, 0);
	OLED_ShowSetExitOpr(0, 2, 1);
	OLED_Fill(0, 4, 16, 1);
	OLED_Fill(0, 6, 16, 1);

}

static void OLED_ShowExitSelect(void *arg)
{
	/* 显示: 1.时间设置        2.日期设置      3.闹钟设置      [4.退出] */
	OLED_ShowSetPWMOpr(0, 0, 1);
	OLED_ShowSetExitOpr(0, 2, 0);
	OLED_Fill(0, 4, 16, 1);
	OLED_Fill(0, 6, 16, 1);
}


static void OLED_ShowTimeSetting_Hour(void *arg)
{
	/* 时间设置:选中时 */
	u8 index[] = {14, 15, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD) 
	{
		g_tTimeTmp.hour++;
		if (g_tTimeTmp.hour == 24) g_tTimeTmp.hour = 0;
	}
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_tTimeTmp.hour--;
		if (g_tTimeTmp.hour == 255) g_tTimeTmp.hour = 23; /* 8位无符号数 i= 0, i--的结果为255 */
	}
	
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTimeSelet(&g_tTimeTmp, 0);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);
}

static void OLED_ShowTimeSetting_Min(void *arg)
{
	/* 时间设置:选中分 */
	u8 index[] = {14, 15, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD)
	{
		g_tTimeTmp.min++;
		if (g_tTimeTmp.min == 60) g_tTimeTmp.min = 0;
	}
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_tTimeTmp.min--;
		if (g_tTimeTmp.min == 255) g_tTimeTmp.min = 59;
	}
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTimeSelet(&g_tTimeTmp, 1);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);
}

static void OLED_ShowTimeSetting_Sec(void *arg)
{
	/* 时间设置:选中秒 */
	u8 index[] = {14, 15, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD) 
	{
		g_tTimeTmp.sec++;
		if (g_tTimeTmp.sec == 60) g_tTimeTmp.sec = 0;
	}
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_tTimeTmp.sec--;
		if (g_tTimeTmp.sec == 255) g_tTimeTmp.sec = 59;
	}

	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTimeSelet(&g_tTimeTmp, 2);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);
}

static void OLED_ShowTimeSetting_Save(void *arg)
{
	/* 时间设置:选中保存 */
	u8 index[] = {14, 15, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ENTER)
	{
		RTC_Set(g_tTimeTmp.year, g_tTimeTmp.moth, g_tTimeTmp.day, g_tTimeTmp.hour, g_tTimeTmp.min, g_tTimeTmp.sec);
	}

	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTime((128 - 16*8)/2, 2, 32, &g_tTimeTmp);
	OLED_ShowSave(0, 6, 0);
	OLED_ShowExit((128 - 16*2),6, 1);
}

static void OLED_ShowTimeSetting_Exit(void *arg)
{
	/* 时间设置:选中退出 */
	u8 index[] = {14, 15, 16, 17};

	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTime((128 - 16*8)/2, 2, 32, &g_tTimeTmp);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 0);
}

static void OLED_ShowDateSelet(ptTime pTime, u8 select)
{
	u8 buf[5];

	u8 posx1 = (128 - 16 * 7) / 2;

	switch (select)
	{
		case 0:  /* 选择年份 */
		{
			sprintf((char*)buf,"%04d", (int)pTime->year);
			OLED_ShowString(posx1, 3, buf, 16, 0);
			OLED_ShowChinese(posx1 + 32, 3, 0, 16, 1);   /* 年 */
			sprintf((char*)buf, "%02d", (int)pTime->moth);
	        OLED_ShowString(posx1 + 48, 3, buf, 16, 1);
			OLED_ShowChinese(posx1 + 64, 3, 1, 16, 1);    /* 月 */
			sprintf((char*)buf, "%02d", (int)pTime->day);
			OLED_ShowString(posx1 + 80, 3, buf, 16, 1);
			OLED_ShowChinese(posx1 + 96, 3, 10, 16, 1); /* 日 */
			break;
		}

		case 1: /* 选择月份 */
		{
			sprintf((char*)buf,"%04d", (int)pTime->year);
			OLED_ShowString(posx1, 3, buf, 16, 1);
			OLED_ShowChinese(posx1 + 32, 3, 0, 16, 1);   /* 年 */
			sprintf((char*)buf, "%02d", (int)pTime->moth);
	        OLED_ShowString(posx1 + 48, 3, buf, 16, 0);
			OLED_ShowChinese(posx1 + 64, 3, 1, 16, 1);    /* 月 */
			sprintf((char*)buf, "%02d", (int)pTime->day);
			OLED_ShowString(posx1 + 80, 3, buf, 16, 1);
			OLED_ShowChinese(posx1 + 96, 3, 10, 16, 1); /* 日 */

			break;
		}

		case 2: /* 选择日 */
		{
			sprintf((char*)buf,"%04d", (int)pTime->year);
			OLED_ShowString(posx1, 3, buf, 16, 1);
			OLED_ShowChinese(posx1 + 32, 3, 0, 16, 1);   /* 年 */
			sprintf((char*)buf, "%02d", (int)pTime->moth);
	        OLED_ShowString(posx1 + 48, 3, buf, 16, 1);
			OLED_ShowChinese(posx1 + 64, 3, 1, 16, 1);    /* 月 */
			sprintf((char*)buf, "%02d", (int)pTime->day);
			OLED_ShowString(posx1 + 80, 3, buf, 16, 0);
			OLED_ShowChinese(posx1 + 96, 3, 10, 16, 1); /* 日 */

			break;
		}
		default:
			break;
	}		
}


static void OLED_ShowDateSetting_Year(void *arg)
{
	/* 日期设置:选中年份 */
	u8 index[] = {10, 3, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD)  
	{
		g_tTimeTmp.year++;
		if (g_tTimeTmp.year == 2100) g_tTimeTmp.year = 1901; /* 只允许1901-2099年 */
	}
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_tTimeTmp.year--;
		if (g_tTimeTmp.year == 1900) g_tTimeTmp.year = 2099; 
	}	
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowDateSelet(&g_tTimeTmp, 0);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);
}

static void OLED_ShowDateSetting_Month(void *arg)
{
	/* 日期设置:选中月份 */
	u8 index[] = {10, 3, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD) 
	{
		g_tTimeTmp.moth++;
		if (g_tTimeTmp.moth == 13) g_tTimeTmp.moth = 1;
	}
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_tTimeTmp.moth--;
		if (g_tTimeTmp.moth == 0) g_tTimeTmp.hour = 12; 
	}
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowDateSelet(&g_tTimeTmp, 1);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);	
}

static void OLED_ShowDateSetting_Day(void *arg)
{
	/* 日期设置:选中日 */
	u8 index[] = {10, 3, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD) 
	{
		g_tTimeTmp.day++;

		if (Is_Leap_Year(g_tTimeTmp.year) && g_tTimeTmp.moth == 2 && g_tTimeTmp.day == 30)  g_tTimeTmp.day = 1; /* 闰年月29天 */
		else if (!Is_Leap_Year(g_tTimeTmp.year) && g_tTimeTmp.moth == 2 && g_tTimeTmp.day == 29)  g_tTimeTmp.day = 1; /* 平年月28天 */
		else if ((g_tTimeTmp.moth == 1 || g_tTimeTmp.moth == 3 || g_tTimeTmp.moth == 5 ||\
			g_tTimeTmp.moth == 7 || g_tTimeTmp.moth == 8 || g_tTimeTmp.moth == 10 || g_tTimeTmp.moth == 12) \
			&& g_tTimeTmp.day == 32) g_tTimeTmp.day = 1;       /* 大月31天 */
		else if (g_tTimeTmp.day == 31) g_tTimeTmp.day = 1;     /* 小月30天 */
	} 
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_tTimeTmp.day--;

		if (Is_Leap_Year(g_tTimeTmp.year) && g_tTimeTmp.moth == 2 && g_tTimeTmp.day == 0)  g_tTimeTmp.day = 29; /* 闰年月29天 */
		else if (!Is_Leap_Year(g_tTimeTmp.year) && g_tTimeTmp.moth == 2 && g_tTimeTmp.day == 0)  g_tTimeTmp.day = 28; /* 平年月28天 */
		else if ((g_tTimeTmp.moth == 1 || g_tTimeTmp.moth == 3 || g_tTimeTmp.moth == 5 ||\
			g_tTimeTmp.moth == 7 || g_tTimeTmp.moth == 8 || g_tTimeTmp.moth == 10 || g_tTimeTmp.moth == 12) \
			&& g_tTimeTmp.day == 0) g_tTimeTmp.day = 31;       /* 大月31天 */
		else if (g_tTimeTmp.day == 0) g_tTimeTmp.day = 30;     /* 小月30天 */

	}
	
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowDateSelet(&g_tTimeTmp, 2);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);		
}

static void OLED_ShowDateSetting_Save(void *arg)
{
	/* 日期设置:选中保存 */
	u8 index[] = {10, 3, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ENTER)
	{
		g_tTimeTmp.hour = g_tTime.hour;
		g_tTimeTmp.min = g_tTime.min;
		g_tTimeTmp.sec = g_tTime.sec;
		RTC_Set(g_tTimeTmp.year, g_tTimeTmp.moth, g_tTimeTmp.day, g_tTimeTmp.hour, g_tTimeTmp.min, g_tTimeTmp.sec);
	}
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowYearMothDay((128 - 16*7)/2, 3, 16, &g_tTimeTmp);
	OLED_ShowSave(0, 6, 0);
	OLED_ShowExit((128 - 16*2),6, 1);	

}
static void OLED_ShowDateSetting_Exit(void *arg)
{
	/* 日期设置:选中退出 */
	u8 index[] = {10, 3, 16, 17};
	
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowYearMothDay((128 - 16*7)/2, 3, 16, &g_tTimeTmp);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 0);	
}


static void OLED_ShowAlarmSetting_Hour(void *arg)
{
	/* 闹钟设置:选中时 */
	u8 index[] = {18, 19, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD) 
	{
		g_tAlarmTmp.hour++;
		if (g_tAlarmTmp.hour == 24) g_tAlarmTmp.hour = 0;
	}
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_tAlarmTmp.hour--;
		if (g_tAlarmTmp.hour == 255) g_tAlarmTmp.hour = 23; /* 8位无符号数 i= 0, i--的结果为255 */
	}
	
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTimeSelet(&g_tAlarmTmp, 0);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);	
}

static void OLED_ShowAlarmSetting_Min(void *arg)
{
	/* 闹钟设置:选中分 */
	u8 index[] = {18, 19, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD)
	{
		g_tAlarmTmp.min++;
		if (g_tAlarmTmp.min == 60) g_tAlarmTmp.min = 0;
	}
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_tAlarmTmp.min--;
		if (g_tAlarmTmp.min == 255) g_tAlarmTmp.min = 59;
	}
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTimeSelet(&g_tAlarmTmp, 1);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);
}

static void OLED_ShowAlarmSetting_Sec(void *arg)
{
	/* 闹钟设置:选中秒 */
	u8 index[] = {18, 19, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD) 
	{
		g_tAlarmTmp.sec++;
		if (g_tAlarmTmp.sec == 60) g_tAlarmTmp.sec = 0;
	}
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_tAlarmTmp.sec--;
		if (g_tAlarmTmp.sec == 255) g_tAlarmTmp.sec = 59;
	}

	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTimeSelet(&g_tAlarmTmp, 2);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);

}
static void OLED_ShowAlarmSetting_Save(void *arg)
{
	/* 闹钟设置:选中保存 */
	u8 index[] = {18, 19, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ENTER)
	{
		Alarm_Set(&g_tAlarmTmp);
	}

	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTime((128 - 16*8)/2, 2, 32, &g_tAlarmTmp);
	OLED_ShowSave(0, 6, 0);
	OLED_ShowExit((128 - 16*2),6, 1);

}
static void OLED_ShowAlarmSetting_Exit(void *arg)
{
	/* 闹钟设置:选中退出 */
	u8 index[] = {18, 19, 16, 17};

	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);
	OLED_ShowTime((128 - 16*8)/2, 2, 32, &g_tAlarmTmp);
	OLED_ShowSave(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 0);
}


static void OLED_ShowPlusWith(u8 x, u8 y, u8 mode, u32 ms)
{
	u8 index[] = {24, 26};
	u8 buf[4];
	u8 i = 0;
	
	OLED_ShowChineseString(x, y, index, sizeof(index), 16, 1);
	OLED_ShowChar(x+32, y, ':', 16, 1);
	sprintf((char*)buf,"%d", (int)ms);
	OLED_ShowString(x+40, y, buf, 16, mode);
	while (buf[i] != '\0')
		i++;
 
    OLED_ShowString((x+40 + 8 * i), y, "ms", 16, 1);
	OLED_Fill((x+40 + 8 * i + 16), y, 16, 1);
}

static void OLED_ShowPlusPolarity(u8 x, u8 y, u8 mode, u8 polarity)
{
	u8 index[] = {27, 28};
	
	OLED_ShowChineseString(x, y, index, sizeof(index), 16, 1);
	OLED_ShowChar(x+32, y, ':', 16, 1);
	if(polarity)
	{
		/* 正向 */
	    index[0] = 29;
		OLED_ShowChineseString(x+40, y, index, sizeof(index) - 1, 16, mode);
	}
	else
	{
	   /* 反向 */
       index[0] = 30;
	   OLED_ShowChineseString(x+40, y, index, sizeof(index) - 1, 16, mode);
	}
	OLED_ShowString((x+40+32), y, "OUT:PB7", 16, 1);
}

static void OLED_ShowPlusTrigger(u8 x, u8 y, u8 mode)
{
	u8 index[] = {31, 32};
	OLED_ShowChineseString(x, y, index, sizeof(index), 16, mode);	
}

static void OLED_ShowPlusSetting_Width(void *arg)
{
	/* 脉冲设置 */
	u8 index[] = {24, 25, 16, 17};
	u8 *KeyVal = (u8 *)arg;

	if (*KeyVal == KEY_VALUE_ADD) 
	{
		g_dwPlusWidthMs += 1;
		if (g_dwPlusWidthMs > 100)
			g_dwPlusWidthMs = 0;
	}
	else if (*KeyVal == KEY_VALUE_SUB) 
	{
		g_dwPlusWidthMs -= 1;
		if (g_dwPlusWidthMs == (u32)~(0))
			g_dwPlusWidthMs = 100;
	}

	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);	
	OLED_ShowPlusWith(0, 2, 0, g_dwPlusWidthMs);
	OLED_ShowPlusPolarity(0, 4, 1, g_cPlusPolarity);
	OLED_ShowPlusTrigger(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);
}

static void OLED_ShowPlusSetting_Polarity(void *arg)
{
	/* 脉冲设置 */
	u8 index[] = {24, 25, 16, 17};
	u8 *KeyVal = (u8 *)arg;
	
	if (*KeyVal == KEY_VALUE_ADD || *KeyVal == KEY_VALUE_SUB) 
	{
		if (g_cPlusPolarity == 1)
		{
			g_cPlusPolarity = 0;
			PLUS_OUT = 1;
		}
		else
		{
			g_cPlusPolarity = 1;
			PLUS_OUT = 0;
		}
	}
	
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);	
	
	OLED_ShowPlusWith(0, 2, 1, g_dwPlusWidthMs);
	OLED_ShowPlusPolarity(0, 4, 0, g_cPlusPolarity);
	OLED_ShowPlusTrigger(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 1);	
}

static void OLED_ShowPlusSetting_Trigger(void *arg)
{
	/* 脉冲设置 */
	u8 index[] = {24, 25, 16, 17};
	u8 *KeyVal = (u8 *)arg; 
	
	if (*KeyVal == KEY_VALUE_ADD || *KeyVal == KEY_VALUE_SUB) 
	{	
		OLED_ShowPlusTrigger(0, 6, 1);
		delay_ms(10);
		OLED_ShowPlusTrigger(0, 6, 0);
		
		PLUS_OUT = !PLUS_OUT;
		delay_ms(g_dwPlusWidthMs);
		PLUS_OUT = !PLUS_OUT;
		
	}
	
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);	
	OLED_ShowPlusWith(0, 2, 1, g_dwPlusWidthMs);
	OLED_ShowPlusPolarity(0, 4, 1, g_cPlusPolarity);
	OLED_ShowPlusTrigger(0, 6, 0);
	OLED_ShowExit((128 - 16*2),6, 1);	
	
}
static void OLED_ShowPlusSetting_Exit(void *arg)
{
	/* 脉冲设置 */
	u8 index[] = {24, 25, 16, 17};
	OLED_ShowChineseString((128 - 16 * 4) / 2, 0, index, sizeof(index), 16, 1);	
	
	OLED_ShowPlusWith(0, 2, 1, g_dwPlusWidthMs);
	OLED_ShowPlusPolarity(0, 4, 1, g_cPlusPolarity);
	OLED_ShowPlusTrigger(0, 6, 1);
	OLED_ShowExit((128 - 16*2),6, 0);	
}

static void OLED_ShowFreqCycle(u8 x, u8 y, u8 mode, PT_Signal ptSignal)
{
	u8 index[] = {35, 36};
	u8 buf[10];
	int i = 0;
	OLED_ShowChineseString(x, y, index, sizeof(index), 16, 1);	
	OLED_ShowChar(x+32, y, ':', 16, 1);
	sprintf(buf, "%.2f", 1000/(float)(ptSignal->cycle));
	OLED_ShowString(x+40, y, buf, 16, mode);
	while (buf[i] != '\0')
		i++;
	OLED_ShowString(x+40 + 8*i, y, "Hz", 16, 1);
	OLED_Fill((x+40 + 8 * i + 16), y, 16, 1);
	i = 0;
	
	index[0] = 33;
	index[1] = 34;
	OLED_ShowChineseString(x, y+2, index, sizeof(index), 16, 1);	
	OLED_ShowChar(x+32, y+2, ':', 16, 1);
	sprintf(buf, "%d",ptSignal->cycle);
	OLED_ShowString(x+40, y+2, buf, 16, mode);
	while (buf[i] != '\0')
		i++;
	OLED_ShowString(x+40 + 8*i, y+2, "ms", 16, 1);
	OLED_Fill((x+40 + 8 * i + 16), y+2, 16, 1);
}

static void OLED_ShowDuty(u8 x, u8 y, u8 mode, PT_Signal ptSignal)
{
	u8 index[] = {37, 38, 39};
	u8 buf[10];
	int i = 0;
	
	OLED_ShowChineseString(x, y, index, sizeof(index), 16, 1);
	OLED_ShowChar(x+48, y, ':', 16, 1);
	sprintf(buf, "%d%%", ptSignal->duty);
	OLED_ShowString(x+56, y, buf, 16, mode);
	while (buf[i] != '\0')
		i++;
	OLED_Fill((x+56 + 8 * i), y, 16, 1);
}

static void OLED_ShowPWMSetting_Cycle(void *arg)
{
	
	PT_Signal ptSignal = GetPWMSignal();
	u8 *KeyVal = (u8 *)arg; 

	
	if (*KeyVal == KEY_VALUE_ADD || *KeyVal == KEY_VALUE_SUB) 
	{
		ptSignal->cycle += 10;
		if (ptSignal->cycle > 1000) ptSignal->cycle = 10;
		SetPWMSignal(ptSignal);
	}	
	
	OLED_ShowFreqCycle(0, 0, 0, ptSignal);
	OLED_ShowDuty(0, 4, 1, ptSignal);
	OLED_ShowString(0, 6, "OUT:PB5", 16, 1);
	OLED_ShowExit((128 - 16*2),6, 1);	
	
}
static void OLED_ShowPWMSetting_Duty(void *arg)
{
	PT_Signal ptSignal = GetPWMSignal();
	u8 *KeyVal = (u8 *)arg; 

	
	if (*KeyVal == KEY_VALUE_ADD || *KeyVal == KEY_VALUE_SUB) 
	{
		ptSignal->duty += 25;
		if (ptSignal->duty > 100) ptSignal->duty = 0;
		SetPWMSignal(ptSignal);
	}	
	
	OLED_ShowFreqCycle(0, 0, 1, ptSignal);
	OLED_ShowDuty(0, 4, 0, ptSignal);
	OLED_ShowString(0, 6, "OUT:PB5", 16, 1);
	OLED_ShowExit((128 - 16*2),6, 1);		
}
static void OLED_ShowPWMSetting_Exit(void *arg)
{
	PT_Signal ptSignal = GetPWMSignal();
	
	OLED_ShowFreqCycle(0, 0, 1, ptSignal);
	OLED_ShowDuty(0, 4, 1, ptSignal);
    OLED_ShowString(0, 6, "OUT:PB5", 16, 1);
	OLED_ShowExit((128 - 16*2),6, 0);	
}


/* 是否需要清屏
 * 返回值：1：需要；0：不需要
 */
u8 isClearOLED(u8 indexOld, u8 indexNew)
{
	if (indexOld != indexNew)
		return 1;
	else
		return 0;
}


